import { getuser } from '../../utils/storage'
import { request, wxlogin } from '../../utils/https'
import { sortBy } from 'underscore'
import * as moment from 'moment'
import { getschoolstudenthomework } from '../../utils/interfaces'
export default {
  data () {
    return {
      movies: [
        {
          url:
            'https://hterobot-kimage.oss-cn-qingdao.aliyuncs.com/images1/banner@2x.png'
        },
        {
          url:
            'https://hterobot-kimage.oss-cn-qingdao.aliyuncs.com/images1/banner@2x.png'
        },
        {
          url:
            'https://hterobot-kimage.oss-cn-qingdao.aliyuncs.com/images1/banner@2x.png'
        }
      ],
      current: 'tab1',
      tab11: true,
      tab12: false,
      tab13: false,
      operation: [],
      currentPage: 1,
      oldList: [],
      logStatus: 0,
      homework: []
    }
  },
  computed: {
    gethomework () {
      const { homework } = this
      for (let v of homework) {
        v.time = moment(v.createdAt).format('YYYY-MM-DD hh:mm')
      }
      if (homework.length) {
        const x = []
        const 已完成 = []
        const 已点评 = []
        for (const v of homework) {
          if (['未接收', '已接收'].includes(v.type)) {
            x.push(v)
          } else if (v.type === '已提交') {
            已完成.push(v)
          } else {
            已点评.push(v)
          }
        }
        const 待完成 = sortBy(x, v => v.createdAt)

        return {
          待完成,
          已完成,
          已点评
        }
      }
      return {
        待完成: [],
        已完成: [],
        已点评: []
      }
    }
  },
  onShow () {
    this.start()
  },
  methods: {
    async start () {
      const { ID } = await getuser('user')
      try {
        let code = await wxlogin()
        this.homework = await request(getschoolstudenthomework, {
          data: {
            schoolstudentid: ID,
            code
          }
        })
      } catch (err) {
        wx.showModal({
          title: '提示',
          content: '服务器请求失败，点击确定重新加载',
          success: res => {
            if (res.confirm) {
              this.start()
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    },
    handleChange (detail) {
      this.current = detail.mp.detail.key
    },
    pendings ({ID, type}) {
      if (type === '已提交') {
        wx.navigateTo({
          url: `../../pages/Completejob/main?ID=${ID}`
        })
      } else if (type === '已点评') {
        wx.navigateTo({
          url: `../../pages/assignment/main?ID=${ID}`
        })
      } else {
        wx.navigateTo({
          url: `../../pages/Writehomework/main?ID=${ID}`
        })
      }
    },
    gettime (time) {
      return moment(time).format('YYYY-MM-DD hh:mm')
    }
  }
}
